﻿using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x02000408 RID: 1032
public class UVNormalBlend : MonoBehaviour
{
	// Token: 0x17000275 RID: 629
	// (get) Token: 0x060019C4 RID: 6596 RVA: 0x000A5B64 File Offset: 0x000A3D64
	// (set) Token: 0x060019C5 RID: 6597 RVA: 0x000A5B6C File Offset: 0x000A3D6C
	public float Rate
	{
		get
		{
			return this.rate;
		}
		set
		{
			this.rate = value;
			this.Calc();
		}
	}

	// Token: 0x17000276 RID: 630
	// (get) Token: 0x060019C6 RID: 6598 RVA: 0x000A5B7C File Offset: 0x000A3D7C
	// (set) Token: 0x060019C7 RID: 6599 RVA: 0x000A5B84 File Offset: 0x000A3D84
	public bool IsSetuped { get; private set; }

	// Token: 0x060019C8 RID: 6600 RVA: 0x000A5B90 File Offset: 0x000A3D90
	private void Awake()
	{
		if (!this.IsSetuped)
		{
			this.Setup();
		}
		this.Calc();
	}

	// Token: 0x060019C9 RID: 6601 RVA: 0x000A5BAC File Offset: 0x000A3DAC
	private void Update()
	{
	}

	// Token: 0x060019CA RID: 6602 RVA: 0x000A5BB0 File Offset: 0x000A3DB0
	public void Setup()
	{
		for (int i = 0; i < this.datas.Length; i++)
		{
			this.SetupMesh(this.datas[i]);
		}
		this.IsSetuped = true;
	}

	// Token: 0x060019CB RID: 6603 RVA: 0x000A5BEC File Offset: 0x000A3DEC
	private void SetupMesh(UVNormalBlend.Data data)
	{
		data.cloneMesh = UVNormalBlend.CloneMesh(data.renderer.gameObject);
	}

	// Token: 0x060019CC RID: 6604 RVA: 0x000A5C04 File Offset: 0x000A3E04
	private void Calc()
	{
		for (int i = 0; i < this.datas.Length; i++)
		{
			this.CalcData(this.datas[i]);
		}
	}

	// Token: 0x060019CD RID: 6605 RVA: 0x000A5C38 File Offset: 0x000A3E38
	private void CalcData(UVNormalBlend.Data data)
	{
		if (this.changeNormal)
		{
			if (data.cloneMesh.normals.Length == data.baseNormals.Length)
			{
				Vector3[] array = new Vector3[data.baseNormals.Length];
				for (int i = 0; i < data.baseNormals.Length; i++)
				{
					array[i] = Vector3.Lerp(data.baseNormals[i], data.blendNormals[i], this.rate);
				}
				data.cloneMesh.normals = array;
			}
			else
			{
				GlobalData.Debug_LogWarning("法线的数量不同");
			}
		}
		if (this.changeUV)
		{
			if (data.cloneMesh.uv.Length == data.baseUVs.Length)
			{
				Vector2[] array2 = new Vector2[data.baseUVs.Length];
				for (int j = 0; j < data.baseUVs.Length; j++)
				{
					array2[j] = Vector3.Lerp(data.baseUVs[j], data.blendUVs[j], this.rate);
				}
				data.cloneMesh.uv = array2;
			}
			else
			{
				GlobalData.Debug_LogWarning("UV的数量不同");
			}
		}
	}

	// Token: 0x060019CE RID: 6606 RVA: 0x000A5D94 File Offset: 0x000A3F94
	public void Change(float rate)
	{
		this.rate = rate;
		this.Calc();
	}

	// Token: 0x060019CF RID: 6607 RVA: 0x000A5DA4 File Offset: 0x000A3FA4
	public void CreateData(Transform trans)
	{
		Renderer[] componentsInChildren = base.transform.GetComponentsInChildren<Renderer>(true);
		Renderer[] componentsInChildren2 = trans.GetComponentsInChildren<Renderer>(true);
		List<UVNormalBlend.Data> list = new List<UVNormalBlend.Data>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			for (int j = 0; j < componentsInChildren2.Length; j++)
			{
				if (componentsInChildren[i].name == componentsInChildren2[j].name)
				{
					UVNormalBlend.Data data = this.CreateData(componentsInChildren[i], componentsInChildren2[j]);
					if (data != null)
					{
						list.Add(data);
					}
				}
			}
		}
		this.datas = list.ToArray();
		MonoBehaviour.print("データ作成完了:" + this.datas.Length + "個");
	}

	// Token: 0x060019D0 RID: 6608 RVA: 0x000A5E60 File Offset: 0x000A4060
	public void CreateData(NormalData blendData)
	{
		Renderer[] componentsInChildren = base.transform.GetComponentsInChildren<Renderer>(true);
		List<UVNormalBlend.Data> list = new List<UVNormalBlend.Data>();
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			for (int j = 0; j < blendData.data.Count; j++)
			{
				if (componentsInChildren[i].name == blendData.data[j].ObjectName)
				{
					UVNormalBlend.Data data = this.CreateData(componentsInChildren[i], blendData.data[j]);
					if (data != null)
					{
						list.Add(data);
					}
				}
			}
		}
		this.datas = list.ToArray();
		this.changeUV = false;
		MonoBehaviour.print("データ作成完了:" + this.datas.Length + "個");
	}

	// Token: 0x060019D1 RID: 6609 RVA: 0x000A5F30 File Offset: 0x000A4130
	private UVNormalBlend.Data CreateData(Renderer baseRend, Renderer blendRend)
	{
		UVNormalBlend.Data data = new UVNormalBlend.Data();
		Mesh mesh = UVNormalBlend.GetMesh(baseRend.gameObject);
		Mesh mesh2 = UVNormalBlend.GetMesh(blendRend.gameObject);
		data.rendererName = baseRend.name;
		data.renderer = baseRend;
		data.baseNormals = mesh.normals;
		data.baseUVs = mesh.uv;
		data.blendNormals = mesh2.normals;
		data.blendUVs = mesh2.uv;
		if (data.baseNormals.Length != data.blendNormals.Length || data.baseUVs.Length != data.blendUVs.Length)
		{
			GlobalData.Debug_LogWarning("网格混合的数据数量不一致");
			return null;
		}
		return data;
	}

	// Token: 0x060019D2 RID: 6610 RVA: 0x000A5FD8 File Offset: 0x000A41D8
	private UVNormalBlend.Data CreateData(Renderer rend, NormalData.Param param)
	{
		UVNormalBlend.Data data = new UVNormalBlend.Data();
		data.rendererName = param.ObjectName;
		data.renderer = rend;
		data.baseNormals = param.NormalMax.ToArray();
		data.baseUVs = null;
		data.blendNormals = param.NormalMin.ToArray();
		data.blendUVs = null;
		if (data.baseNormals.Length != data.blendNormals.Length)
		{
			GlobalData.Debug_LogWarning("网格混合的数据数量不一致");
			return null;
		}
		return data;
	}

	// Token: 0x060019D3 RID: 6611 RVA: 0x000A6050 File Offset: 0x000A4250
	public static Mesh CloneMesh(GameObject OriginalData)
	{
		MeshFilter meshFilter = OriginalData.GetComponent(typeof(MeshFilter)) as MeshFilter;
		if (null != meshFilter)
		{
			Mesh mesh = UnityEngine.Object.Instantiate<Mesh>(meshFilter.sharedMesh);
			mesh.name = meshFilter.sharedMesh.name;
			meshFilter.sharedMesh = mesh;
			return meshFilter.sharedMesh;
		}
		SkinnedMeshRenderer component = OriginalData.GetComponent<SkinnedMeshRenderer>();
		if (component && component.sharedMesh)
		{
			Mesh mesh2 = UnityEngine.Object.Instantiate<Mesh>(component.sharedMesh);
			mesh2.name = component.sharedMesh.name;
			component.sharedMesh = mesh2;
			return component.sharedMesh;
		}
		return null;
	}

	// Token: 0x060019D4 RID: 6612 RVA: 0x000A60F8 File Offset: 0x000A42F8
	public static Mesh GetMesh(GameObject OriginalData)
	{
		MeshFilter component = OriginalData.GetComponent<MeshFilter>();
		if (null != component)
		{
			return component.sharedMesh;
		}
		SkinnedMeshRenderer component2 = OriginalData.GetComponent<SkinnedMeshRenderer>();
		if (component2 && component2.sharedMesh)
		{
			return component2.sharedMesh;
		}
		return null;
	}

	// Token: 0x060019D5 RID: 6613 RVA: 0x000A614C File Offset: 0x000A434C
	public Mesh GetClonedMesh(string name)
	{
		for (int i = 0; i < this.datas.Length; i++)
		{
			if (this.datas[i].rendererName == name)
			{
				return this.datas[i].cloneMesh;
			}
		}
		return null;
	}

	// Token: 0x04001CC8 RID: 7368
	private float rate;

	// Token: 0x04001CC9 RID: 7369
	[SerializeField]
	private bool changeUV = true;

	// Token: 0x04001CCA RID: 7370
	[SerializeField]
	private bool changeNormal = true;

	// Token: 0x04001CCB RID: 7371
	[SerializeField]
	private UVNormalBlend.Data[] datas;

	// Token: 0x02000409 RID: 1033
	[Serializable]
	public class Data
	{
		// Token: 0x17000277 RID: 631
		// (get) Token: 0x060019D7 RID: 6615 RVA: 0x000A61A4 File Offset: 0x000A43A4
		// (set) Token: 0x060019D8 RID: 6616 RVA: 0x000A61AC File Offset: 0x000A43AC
		public Mesh cloneMesh { get; set; }

		// Token: 0x04001CCD RID: 7373
		public string rendererName;

		// Token: 0x04001CCE RID: 7374
		public Renderer renderer;

		// Token: 0x04001CCF RID: 7375
		public Vector3[] baseNormals;

		// Token: 0x04001CD0 RID: 7376
		public Vector3[] blendNormals;

		// Token: 0x04001CD1 RID: 7377
		public Vector2[] baseUVs;

		// Token: 0x04001CD2 RID: 7378
		public Vector2[] blendUVs;
	}
}
